package com.xiaobai.oper;

import com.xiaobai.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

/*
线程统筹---华罗庚-烧水泡茶
 */
@Slf4j
public class ThreadCoordination {
    public static void main(String[] args) {
        Coordination2 coordination = new Coordination2();
        coordination.makeTea();
    }
}

@Slf4j
class Coordination {
    Thread t1 = new Thread("老李") {
        @Override
        public void run() {
            log.debug("开始洗茶壶...");
            ThreadUtil.sleep(1);
            log.debug("结束洗茶壶...");
            log.debug("开始洗茶杯...");
            ThreadUtil.sleep(2);
            log.debug("结束洗茶杯...");
            log.debug("开始拿茶叶...");
            ThreadUtil.sleep(1);
            log.debug("结束拿茶叶...");
            try {
                t2.join();
                log.debug("泡茶...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    };
    Thread t2 = new Thread("老王") {
        @Override
        public void run() {
            log.debug("洗水壶...");
            ThreadUtil.sleep(1);
            log.debug("洗水壶完成...");
            log.debug("开始烧开水...");
            ThreadUtil.sleep(15);
            log.debug("结束烧开水...");

        }
    };

    public void makeTea() {
        t1.start();
        t2.start();
    }
}


@Slf4j
class Coordination2 {
    Thread t1 = new Thread("老李") {
        @Override
        public void run() {
            log.debug("开始洗茶壶...");
            ThreadUtil.sleep(1);
            log.debug("结束洗茶壶...");
            log.debug("开始洗茶杯...");
            ThreadUtil.sleep(2);
            log.debug("结束洗茶杯...");
            log.debug("开始拿茶叶...");
            ThreadUtil.sleep(1);
            log.debug("结束拿茶叶...");
        }
    };
    Thread t2 = new Thread("老王") {
        @Override
        public void run() {
            log.debug("洗水壶...");
            ThreadUtil.sleep(1);
            log.debug("洗水壶完成...");
            log.debug("开始烧开水...");
            ThreadUtil.sleep(15);
            log.debug("结束烧开水...");
            try {
                t1.join(); // 老王等待老李完成所有任务
                log.debug("泡茶...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    };

    public void makeTea() {
        t1.start();
        t2.start();
    }
}